package com.jpress.filtros;

import java.io.IOException;
import java.util.List;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.jpress.facade.Facade;
import com.jpress.model.Blog;
import com.jpress.model.Usuario;

@WebFilter({ 
	"/ListaBlogsFilter", 
	"/postnovo.jsp", 
	"/custompagenova.jsp",
	"/categorianova.jsp"
})
public class ListaBlogsFilter implements Filter {

    public ListaBlogsFilter() {}

	public void destroy() {}

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		Facade facade = new Facade();
		HttpServletRequest httpServletReq = (HttpServletRequest) request;
		HttpSession session = httpServletReq.getSession();
		Usuario logado = (Usuario) session.getAttribute("logado");
		if(logado != null){
			List<Blog> meusBlogs = facade.meusBlogs(logado.getId());
			httpServletReq.setAttribute("meusBlogs", meusBlogs);
		}
		chain.doFilter(request, response);
	}

	public void init(FilterConfig fConfig) throws ServletException {}

}
